package com.test.prolenders.adapter_prolenders;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.prolenders.R;
import com.test.prolenders.bean_prolenders.PermissionInfo_prolenders;

import java.util.ArrayList;


public class PermissionViewAdapter_prolenders extends RecyclerView.Adapter<PermissionViewAdapter_prolenders.PermissionDialogHolder> {

    private final ArrayList<PermissionInfo_prolenders> infos;

    public PermissionViewAdapter_prolenders(ArrayList<PermissionInfo_prolenders> infos){
        this.infos = infos;
    }

    public PermissionInfo_prolenders getInfo(int index){
        return infos.get(index);
    }

    @NonNull
    @Override
    public PermissionDialogHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PermissionDialogHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_permission_prolenders,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull PermissionDialogHolder holder, int position) {

        PermissionInfo_prolenders info = infos.get(position);

        if (info.getIconResId()!=null) holder.setIcon(info.getIconResId());
        holder.setTitle(info.getTitle());
        holder.setText(info.getText());
        info.setHolder(holder);

        if (position>=(infos.size()-1)) holder.getNextView().setVisibility(View.GONE);
        else holder.getNextView().setOnClickListener(v -> info.next());

    }

    @Override
    public int getItemCount() {
        return infos.size();
    }

    public static class PermissionDialogHolder extends RecyclerView.ViewHolder{

        private final ImageView imgIv;
        private final TextView titleTv;
        private final TextView textTv;
        private final View nextView;

        @SuppressLint("InflateParams")
        public PermissionDialogHolder(View view){

            super(view);

            imgIv = view.findViewById(R.id.ip_iv_img);
            titleTv = view.findViewById(R.id.ip_tv_title);
            textTv = view.findViewById(R.id.ip_tv_text);
            nextView = view.findViewById(R.id.ip_view_next);

        }

        public void setIcon(int iconresid){
            imgIv.setImageResource(iconresid);
        }

        public void setTitle(String title) {
            titleTv.setText(title);
        }

        public void setText(String text){
            textTv.setText(text);
        }

        public View getNextView() {
            return nextView;
        }
    }
}
